/**
 * 资源分配
 */
Ext.define('BenBen.modules.system.resource.ResourceDistributeWindow', {
    extend : 'Ext.window.Window',
    requires: [
        'BenBen.modules.system.resource.ResourceTreeGrid'
    ],
    width : 500,
    height : 500,
    title : '资源分配',
    border : false,
	initComponent : function() {
		var me = this;

		var resourceTreePanel =  Ext.create('BenBen.modules.system.resource.ResourceTreeGrid',{
			isDistribute : true,
			isLoad : false,
			roleId :  me.roleRecord.get('id')
			
		});
		
		var bbar = new Ext.Toolbar({
            items : [{
                xtype: 'tbfill'
            },{
                text : '确定',
                iconCls : 'formsave',
                handler : function() {	
                	var records = resourceTreePanel.getView().getChecked(),
                     ids = [];
                	Ext.Array.each(records, function(rec){
                        ids.push(rec.get('id'));
                    });
                	Ext.Ajax.request({
                        url: 'system/role/resources',
                        method: 'POST',
                        params : {
                        	id : me.roleRecord.get('id'),
                        	resourceIds : ids.join(',')
                        },
                        callback: function (options, success, response) {
                            if (success) {
                                if (Ext.decode(response.responseText).success) {
                                	Ext.Msg.confirm('成功','操作成功');
        						} else {
        							Ext.Msg.confirm('失败',Ext.decode(response.responseText).info);
        						}
                                me.hide();
                            } else {
                                Ext.Msg.confirm('失败','操作失败');
                            }
                        }
                    });
                }
            }, {
                text : '取消',
                iconCls : 'formcancle',
                handler : function() {
                	me.hide();
                }
            },{
                xtype: 'tbfill'
            }]
        });


		Ext.apply(this, {
			maximizable : true,
			closeAction : 'hide',
            border : false,
            bbar : bbar,
            title : me.roleRecord.get('name')+'资源',
            layout : 'form',
			items : [resourceTreePanel]
		});
		this.callParent(arguments);
	}

});